<?php
/////////////////////////////////////////////////////////////////
// SpeedPHP中文PHP框架, Copyright (C) 2008 - 2010 SpeedPHP.com //
/////////////////////////////////////////////////////////////////

$__speedy_compression_level = 9;

/**
 * speedy 简单的PHP模板引擎，仅通过PHP本身来作为模板的语法。让开发者除了Smarty等模板引擎之外，可以有一个快速并且简单的模板引擎方案。
 *
 * speedy拥有的功能：按模板路径来获取模板并显示，通过assign对模板内变量进行赋值，检查模板文件是否存在，GZip压缩等。
 */
class speedy{
	public static $pager = null;
	public static $data = null;
	/**
	 * 模板解析函数
	 */
	public static function getlist($type,$cid=0,$num=10,$order="id DESC"){
		if(!in_array($type, array('news','prod','zzhu')))	return false;
		if((int)$cid == 0){
			$where = array('visible'=>1);
		}else{
			$where = array('visible'=>1,'cid'=>$cid);
		}
		$page = max(1, (int)$_GET['page']);
		switch($type){
			case 'news':
				$data = spClass('m_news')->spPager($page,$num)->findAll($where, $order);
				$pagers = spClass('m_news')->spPager()->getPager();
				$url_r = spUrl('main', 'news', array('cid'=>$cid));
				break;
			case 'prod':
				$data = spClass('m_prod')->spPager($page,$num)->findAll($where, $order);
				$pagers = spClass('m_prod')->spPager()->getPager();
				$url_r = spUrl('main', 'prod', array('cid'=>$cid));
				break;
			case 'zzhu':
				$zzcat = spClass('m_zzcat')->find(array('id'=>$cid));
				$modelid = $zzcat['modelid'];
				$table = spClass('m_table')->find(array('modelid'=>$modelid));
				$tablename = $GLOBALS['G_SP']['db']['prefix'] . $table['tablename'];
				$data = spDB($tablename,'id')->spPager($page,$num)->findAll($where, $order);
				$pagers = spDB($tablename,'id')->spPager()->getPager();
				$url_r = spUrl('main', 'zzhu', array('cid'=>$cid));
				break;
		}
		self::$pager = self::getpager($url_r, $pagers);
		self::$data = $data;
	}
	public static function getlist2($type,$num=10,$order="id DESC"){
		if(!in_array($type, array('zpin','mess')))	return false;
		$page = (int) $_GET('page') > 1 ? (int) $_GET('page') : 1;
		switch($type){
			case 'zpin':
				$data = spClass('m_rlzy')->spPager($page,$num)->findAll(null,$order);
				$pagers = spClass('m_rlzy')->spPager()->getPager();
				$url_r = spUrl('main', 'zpin');
				break;
			case 'mess':
				$data = spClass('m_mess')->spPager($page,$num)->findAll(null,$order);
				$pagers = spClass('m_mess')->spPager()->getPager();
				$url_r = spUrl('main', 'mess');
				break;
		}
		self::$pager = self::getpager($url_r, $pagers);
		self::$data = $data;
	}
	public static function getpager($urlfunc, $pager){
		if(empty($pager) || empty($urlfunc))	return false;
		$parse_pager = <<<EOT
每页{$pager['page_size']}条/共{$pager['total_count']}条记录&nbsp;|&nbsp;当前{$pager['current_page']}/{$pager['total_page']}页&nbsp;|
<a href="{$urlfunc}?page={$pager['first_page']}">首页</a>&nbsp;|&nbsp;<a href="{$urlfunc}?page={$pager['prev_page']}">上页</a>&nbsp;|
<a href="{$urlfunc}?page={$pager['next_page']}">下页</a>&nbsp;|&nbsp;<a href="{$urlfunc}?page={$pager['last_page']}">末页</a>&nbsp;|
跳转到第<input type="text" id="gopage" value="1" />页&nbsp;|
点击<a href="javascript:;" onclick="location.href='{$urlfunc}?page='+document.getElementById('gopage').value">此处</a>跳转
EOT;
		return $parse_pager;
	}
	/**
	 * 模板目录
	 */
	public $template_dir = null;
	/**
	 * 是否开启GZip压缩
	 */
	public $enable_gzip	= FALSE;
	/**
	 * GZip压缩级别
	 */
	public $compression_level	=  9;
	/**
	 * 不检查编译目录
	 */
	public $no_compile_dir = true;
	/**
	 * 模板内使用的变量值
	 */
	private $_vars = array();
	
	/**
	 * 对模板赋值
	 * @param key 变量名称，或变量数组
	 * @param value 变量值
	 */
	public function assign($key, $value = null){
		if (is_array($key)){
			foreach($key as $var => $val)if($var != "")$this->_vars[$var] = $val;
		}else{
			if ($key != "")$this->_vars[$key] = $value;
		}
	}
	
	/**
	 * 检测模板是否存在
	 * @param tplname 模板名称
	 */	
	public function templateExists($tplname){
		if (is_readable(realpath($this->template_dir).'/'.$tplname))return TRUE;
		if (is_readable($tplname))return TRUE;
		return FALSE;
	}
	
	/**
	 * templateExists 别名,检测模板是否存在
	 * @param tplname 模板名称
	 */	
	public function template_exists($tplname){return $this->templateExists($tplname);}
	/** 兼容Smarty3*/
	public function registerPlugin(){}
	
	/**
	 * 显示模板
	 * @param tplname 模板名称
	 */	
	public function display($tplname){
		if(is_readable(realpath($this->template_dir).'/'.$tplname)){
			$tplpath = realpath($this->template_dir).'/'.$tplname;
		}elseif(is_readable($tplname)){
			$tplpath = $tplname;
		}else{
			spError("speedy引擎：无法找到模板 ".$tplname);
		}
		extract($this->_vars);
		if( TRUE == $this->enable_gzip ){
			GLOBAL $__speedy_compression_level;
			$__speedy_compression_level = $this->compression_level;
			ob_start('speedy_ob_gzip');
		}
		include $tplpath;
	}
}

function speedy_ob_gzip($content){ 
	if( !headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip") ){
		GLOBAL $__speedy_compression_level;
		$content = gzencode($content,$__speedy_compression_level); 
		header("Content-Encoding: gzip"); 
		header("Vary: Accept-Encoding"); 
		header("Content-Length: ".strlen($content)); 
	} 
	return $content; 
}